{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67d34524-7ce3-41dc-8c7b-2d7c81f12f75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "耐克的口号是: Just do it\n",
      "耐克的口号是: Just do it\n"
     ]
    }
   ],
   "source": [
    "brand = ['李宁', '耐克', '阿迪达斯']\n",
    "slogan = ['一切皆有可能', 'Just do it', 'Impossible is nothing']\n",
    "print('耐克的口号是:', slogan[brand.index('耐克')])  \n",
    "# 耐克的口号是: Just do it\n",
    "\n",
    "dic = {'李宁': '一切皆有可能', '耐克': 'Just do it', '阿迪达斯': 'Impossible is nothing'}\n",
    "print('耐克的口号是:', dic['耐克'])  \n",
    "# 耐克的口号是: Just do it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a2443f3-2793-4b53-a571-a9e7a4fe8107",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 'one', 2: 'two', 3: 'three'}\n",
      "one\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "4",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 4\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(dic1)  \u001b[38;5;66;03m# {1: 'one', 2: 'two', 3: 'three'}\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(dic1[\u001b[38;5;241m1\u001b[39m])  \u001b[38;5;66;03m# one\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mdic1\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m]\u001b[49m)  \u001b[38;5;66;03m# KeyError: 4\u001b[39;00m\n",
      "\u001b[1;31mKeyError\u001b[0m: 4"
     ]
    }
   ],
   "source": [
    "dic1 = {1: 'one', 2: 'two', 3: 'three'}\n",
    "print(dic1)  # {1: 'one', 2: 'two', 3: 'three'}\n",
    "print(dic1[1])  # one\n",
    "print(dic1[4])  # KeyError: 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6f5adced-06d0-4fc9-af8f-82e57ed48540",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'rice': 35, 'wheat': 101, 'corn': 67}\n",
      "35\n"
     ]
    }
   ],
   "source": [
    "dic2 = {'rice': 35, 'wheat': 101, 'corn': 67}\n",
    "print(dic2)  # {'wheat': 101, 'corn': 67, 'rice': 35}\n",
    "print(dic2['rice'])  # 35"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8cca89b2-519f-4f04-a63b-70e436424027",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{(1, 2, 3): 'Tom', 'Age': 12, 3: [3, 5, 7]}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "dic = {(1, 2, 3): \"Tom\", \"Age\": 12, 3: [3, 5, 7]}\n",
    "print(dic)  # {(1, 2, 3): 'Tom', 'Age': 12, 3: [3, 5, 7]}\n",
    "print(type(dic))  # <class 'dict'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29b0b8f3-7027-4757-ac22-66af326b7a64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3}\n",
      "{'a': 11, 'b': 2, 'c': 3}\n",
      "{'a': 11, 'b': 2, 'c': 3, 'd': 4}\n"
     ]
    }
   ],
   "source": [
    "#dict() 创建一个空的字典。\n",
    "#【例子】通过key直接把数据放入字典中，但一个key只能对应一个value，多次对一个key放入 value，后面的值会把前面的值冲掉。\n",
    "\n",
    "dic = dict()\n",
    "dic['a'] = 1\n",
    "dic['b'] = 2\n",
    "dic['c'] = 3\n",
    "\n",
    "print(dic)\n",
    "# {'a': 1, 'b': 2, 'c': 3}\n",
    "\n",
    "dic['a'] = 11\n",
    "print(dic)\n",
    "# {'a': 11, 'b': 2, 'c': 3}\n",
    "\n",
    "dic['d'] = 4\n",
    "print(dic)\n",
    "# {'a': 11, 'b': 2, 'c': 3, 'd': 4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bb46822d-83f3-4c55-939a-3639a14adfbe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple': 4139, 'peach': 4127, 'cherry': 4098}\n",
      "{'apple': 4139, 'peach': 4127, 'cherry': 4098}\n",
      "{'name': 'Tom', 'age': 10}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "dic1 = dict([('apple', 4139), ('peach', 4127), ('cherry', 4098)])\n",
    "print(dic1)  # {'cherry': 4098, 'apple': 4139, 'peach': 4127}\n",
    "\n",
    "dic2 = dict((('apple', 4139), ('peach', 4127), ('cherry', 4098)))\n",
    "print(dic2)  # {'peach': 4127, 'cherry': 4098, 'apple': 4139}\n",
    "\n",
    "dic = dict(name='Tom', age=10)\n",
    "print(dic)  # {'name': 'Tom', 'age': 10}\n",
    "print(type(dic))  # <class 'dict'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "acfaff1c-a2b7-4a88-af4d-9c735eb29287",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': None, 'age': None, 'sex': None}\n",
      "{'name': 10, 'age': 10, 'sex': 10}\n",
      "{'name': ('小马', '8', '男'), 'age': ('小马', '8', '男'), 'sex': ('小马', '8', '男')}\n"
     ]
    }
   ],
   "source": [
    "# dict.fromkeys(seq[, value]) 用于创建一个新字典，以序列 seq 中元素做字典的键，value 为字典所有键对应的初始值。\n",
    "seq = ('name', 'age', 'sex')\n",
    "dic1 = dict.fromkeys(seq)\n",
    "print(dic1)\n",
    "# {'name': None, 'age': None, 'sex': None}\n",
    "\n",
    "dic2 = dict.fromkeys(seq, 10)\n",
    "print(dic2)\n",
    "# {'name': 10, 'age': 10, 'sex': 10}\n",
    "\n",
    "dic3 = dict.fromkeys(seq, ('小马', '8', '男'))\n",
    "print(dic3)\n",
    "# {'name': ('小马', '8', '男'), 'age': ('小马', '8', '男'), 'sex': ('小马', '8', '男')}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bc2455f3-7571-4df0-8915-b322d59ea9db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['Name', 'Age'])\n",
      "['Name', 'Age']\n"
     ]
    }
   ],
   "source": [
    "# dict.keys()返回一个可迭代对象，可以使用 list() 来转换为列表，列表为字典中的所有键。\n",
    "dic = {'Name': 'lsgogroup', 'Age': 7}\n",
    "print(dic.keys())  # dict_keys(['Name', 'Age'])\n",
    "lst = list(dic.keys())  # 转换为列表\n",
    "print(lst)  # ['Name', 'Age']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4cc35529-567c-41d1-ba88-7c14c9108951",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['female', 7, 'Zara'])\n",
      "['female', 7, 'Zara']\n"
     ]
    }
   ],
   "source": [
    "# dict.values()返回一个迭代器，可以使用 list() 来转换为列表，列表为字典中的所有值。\n",
    "dic = {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}\n",
    "print(dic.values())\n",
    "# dict_values(['female', 7, 'Zara'])\n",
    "\n",
    "print(list(dic.values()))\n",
    "# [7, 'female', 'Zara']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "70c301ce-b834-4266-9f82-c238c8fea343",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('Name', 'Lsgogroup'), ('Age', 7)])\n",
      "(('Name', 'Lsgogroup'), ('Age', 7))\n",
      "[('Name', 'Lsgogroup'), ('Age', 7)]\n"
     ]
    }
   ],
   "source": [
    "# dict.items()以列表返回可遍历的 (键, 值) 元组数组。\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "print(dic.items())\n",
    "# dict_items([('Name', 'Lsgogroup'), ('Age', 7)])\n",
    "\n",
    "print(tuple(dic.items()))\n",
    "# (('Name', 'Lsgogroup'), ('Age', 7))\n",
    "\n",
    "print(list(dic.items()))\n",
    "# [('Name', 'Lsgogroup'), ('Age', 7)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9fff6e23-cdab-4166-82f0-62f13e72e18d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 值为 : 27\n",
      "Sex 值为 : NA\n",
      "{'Name': 'Lsgogroup', 'Age': 27}\n"
     ]
    }
   ],
   "source": [
    "# dict.get(key, default=None) 返回指定键的值，如果值不在字典中返回默认值。\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 27}\n",
    "print(\"Age 值为 : %s\" % dic.get('Age'))  # Age 值为 : 27\n",
    "print(\"Sex 值为 : %s\" % dic.get('Sex', \"NA\"))  # Sex 值为 : NA\n",
    "print(dic)  # {'Name': 'Lsgogroup', 'Age': 27}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7c64bb5a-7c83-4da3-9a7b-cee44cdea1b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age 键的值为 : 7\n",
      "Sex 键的值为 : None\n",
      "{'Name': 'Lsgogroup', 'Age': 7, 'Sex': None}\n"
     ]
    }
   ],
   "source": [
    "# dict.setdefault(key, default=None)和get()方法 类似, 如果键不存在于字典中，将会添加键并将值设为默认值。\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "print(\"Age 键的值为 : %s\" % dic.setdefault('Age', None))  # Age 键的值为 : 7\n",
    "print(\"Sex 键的值为 : %s\" % dic.setdefault('Sex', None))  # Sex 键的值为 : None\n",
    "print(dic)  \n",
    "# {'Age': 7, 'Name': 'Lsgogroup', 'Sex': None}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f6dcc419-c622-442c-b830-bc209b7d62d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "键 Age 存在\n",
      "键 Sex 不存在\n",
      "键 Age 存在\n"
     ]
    }
   ],
   "source": [
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "\n",
    "# in 检测键 Age 是否存在\n",
    "if 'Age' in dic:\n",
    "    print(\"键 Age 存在\")\n",
    "else:\n",
    "    print(\"键 Age 不存在\")\n",
    "\n",
    "# 检测键 Sex 是否存在\n",
    "if 'Sex' in dic:\n",
    "    print(\"键 Sex 存在\")\n",
    "else:\n",
    "    print(\"键 Sex 不存在\")\n",
    "\n",
    "# not in 检测键 Age 是否存在\n",
    "if 'Age' not in dic:\n",
    "    print(\"键 Age 不存在\")\n",
    "else:\n",
    "    print(\"键 Age 存在\")\n",
    "\n",
    "# 键 Age 存在\n",
    "# 键 Sex 不存在\n",
    "# 键 Age 存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7de1741-26cd-450d-95a0-22da33ba9d61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a {2: [1, 2]}\n",
      "nokey {2: [1, 2]}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "dic1 = {1: \"a\", 2: [1, 2]}\n",
    "print(dic1.pop(1), dic1)  # a {2: [1, 2]}\n",
    "\n",
    "# 设置默认值，必须添加，否则报错\n",
    "print(dic1.pop(3, \"nokey\"), dic1)  # nokey {2: [1, 2]}\n",
    "\n",
    "del dic1[2]\n",
    "print(dic1)  # {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4108f2d6-5224-46bf-9fd3-b9aacf2c0b6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, [1, 2])\n",
      "{1: 'a'}\n"
     ]
    }
   ],
   "source": [
    "# dict.popitem()随机返回并删除字典中的一对键和值，如果字典已经为空，却调用了此方法，就报出KeyError异常。\n",
    "dic1 = {1: \"a\", 2: [1, 2]}\n",
    "print(dic1.popitem())  # {2: [1, 2]}\n",
    "print(dic1)  # (1, 'a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e0c8e8fa-9c6d-4c34-afbd-d5c97837b93a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字典长度 : 2\n",
      "字典删除后长度 : 0\n"
     ]
    }
   ],
   "source": [
    "# dict.clear()用于删除字典内所有元素。\n",
    "dic = {'Name': 'Zara', 'Age': 7}\n",
    "print(\"字典长度 : %d\" % len(dic))  # 字典长度 : 2\n",
    "dic.clear()\n",
    "print(\"字典删除后长度 : %d\" % len(dic))  \n",
    "# 字典删除后长度 : 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "87875566-af8c-4ef5-8762-cb5ad9476f3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Name': 'Lsgogroup', 'Age': 7, 'Class': 'First'}\n"
     ]
    }
   ],
   "source": [
    "# dict.copy()返回一个字典的浅复制。\n",
    "dic1 = {'Name': 'Lsgogroup', 'Age': 7, 'Class': 'First'}\n",
    "dic2 = dic1.copy()\n",
    "print(dic2)\n",
    "# {'Name': 'Lsgogroup', 'Age': 7, 'Class': 'First'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "95609c63-4768-4162-aa3b-fa4bf3c4e533",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2820443728064\n",
      "2820443728064\n",
      "2820443813824\n",
      "{'user': 'root', 'num': [2, 3]}\n",
      "{'user': 'root', 'num': [2, 3]}\n",
      "{'user': 'lsgogroup', 'num': [2, 3]}\n"
     ]
    }
   ],
   "source": [
    "dic1 = {'user': 'lsgogroup', 'num': [1, 2, 3]}\n",
    "\n",
    "# 引用对象\n",
    "dic2 = dic1  \n",
    "# 浅拷贝父对象（一级目录），子对象（二级目录）不拷贝，还是引用\n",
    "dic3 = dic1.copy()  \n",
    "\n",
    "print(id(dic1))  # 148635574728\n",
    "print(id(dic2))  # 148635574728\n",
    "print(id(dic3))  # 148635574344\n",
    "\n",
    "# 修改 data 数据\n",
    "dic1['user'] = 'root'\n",
    "dic1['num'].remove(1)\n",
    "\n",
    "# 输出结果\n",
    "print(dic1)  # {'user': 'root', 'num': [2, 3]}\n",
    "print(dic2)  # {'user': 'root', 'num': [2, 3]}\n",
    "print(dic3)  # {'user': 'runoob', 'num': [2, 3]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3c5e795a-5b47-4e67-94bd-d87723d07a21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Name': 'Lsgogroup', 'Age': 8, 'Sex': 'female'}\n"
     ]
    }
   ],
   "source": [
    "# dict.update(dict2)把字典参数 dict2 的 key:value对 更新到字典 dict 里。\n",
    "dic = {'Name': 'Lsgogroup', 'Age': 7}\n",
    "dic2 = {'Sex': 'female', 'Age': 8}\n",
    "dic.update(dic2)\n",
    "print(dic)  \n",
    "# {'Sex': 'female', 'Age': 8, 'Name': 'Lsgogroup'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "215a9f98-bd80-4ede-aafc-7a3eebf8b640",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
